<?php
require_once '../../a_config.php';
require_once "{$A_CONFIG['api_include']}";
require_once '../../api_header.php';
$conn = connect_to_db();
$err = -1;
$msg = "";
$condition=array();
if (isset($_POST['token']) && !empty($_POST["token"]) &&
    isset($_POST['time']) && !empty($_POST["time"]) &&
    isset($_POST['search']) ) {
    $key = $A_CONFIG['api_key'];
    $unixtime = $_POST['time'];
    $token = md5($key . $unixtime);
    if ($token == $_POST["token"]) {
        $search = mysql_real_escape_string($_POST["search"]);
        $regSpace = '/^\s+$/';
        if($search!="" && !preg_match($regSpace,$search)){
            $condition[]="(`box_id` like '%{$search}%' )";
        }
        $condition[]="is_del=0";
        $where = '';
        if(!empty($condition)){
            foreach($condition as $key=>$val){
                if($key==0){
                    $where .= "where $val";
                }else{
                    $where .= " and $val";
                }
            }
        }
        $s_db_name=$A_CONFIG['project_pre'] . '_tcp_status';
        $sql = "SELECT count(id) as all_count FROM `{$s_db_name}` $where";
        $res = mysql_query($sql, $conn);
        if(is_resource($res)){
            $row = mysql_fetch_array($res,MYSQL_ASSOC);
            $total_user = (int)$row["all_count"];
            $total_page = ceil($total_user/10);
            $info = array();
            for($i=1;$i<=$total_page;$i++){
                $item = array();
                $item["page"] = $i;
                $info[] = $item;
            }
            $err =0;
            $msg=$info;
        }else{
            $msg = "数据处理出错";
        }
    }else{
        $msg = "用户网点获取失败，请刷新页面";
    }

} else {
    $err = -5;
    $msg = "参数错误";
}
if(!$A_CONFIG['is_debug']){
    $resArr = compact("err", "msg",'total_page');
}else{
    $resArr = compact("err", "msg",'sql','total_page');
}

$resJson = json_encode_cn($resArr);
echo $resJson;